<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        label.required:after { /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
            content: ' *';
            color: red;
        }

        .layui-form-label {
            width: 50px;
            padding: 0px;
            text-align: left;
            height: 45px;
            line-height: 45px;
            font-size: 15px;
        }

        .layui-input-block {
            margin-left: 50px;
        }

        .layui-input {
            height: 45px;
            font-size: 15px;
        }

        .bottom {
            line-height: 0px;
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .button-container {
            position: absolute;
            right: 20px;
        }

        .pear-btn {
            border-radius: 4px;
            font-size: 18px;
        }

        .dtree-select {
            width: 99.5%;
            top: 46px
        }
        .pear-btn i {
            font-size: 15px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label required">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" lay-verify="required" lay-reqtext="请填写账号"
                               autocomplete="off"
                               placeholder="请输入账号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickName" autocomplete="off" placeholder="请输入姓名"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" lay-verify="required" lay-reqtext="请填写密码"
                               autocomplete="off"
                               placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" autocomplete="off" placeholder="请输入电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value=true title="激活" checked>
                        <input type="radio" name="status" value=false title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">
                        部门
                    </label>
                    <div class="layui-input-block">
                        <input type="number" id="deptId" name="deptId" lay-verify="required" lay-reqtext="请选择部门"
                               style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>
                <input type="text" id="roleId" name="roleId" style="display:none;"/>
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色</label>
                    <div class="layui-input-block show-role-container">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" style="margin-right: 20px" class="pear-btn pear-btn-primary " lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;

        // dtree插件下拉框点击空白处隐藏
        $(document).bind("click", function (e) {
            var e = e || window.event; //事件对象，兼容IE
            var target = e.target || e.srcElement; //源对象，兼容火狐和IE
            if ($(target).prop("class").indexOf("layui-card") == -1) {
                $(".dtree-select-show").removeClass("dtree-select-show");
            }
        });
        // 初始化部门树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",//默认展开层级为1
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/my/dept/buildDeptAll",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "不选默认是顶级目录",
            selectCardHeight: "150"
        });
        dtree.on("node('dataTree')", function (obj) {
            var param = dtree.getNowParam("dataTree");
            $("#deptId").val(param.nodeId);
        });
        //获取所有的角色列表
        getAllRole(function () {
            layui.form.render('radio')
        });
        form.on('submit(user-save)', function (data) {
            var rolelRadio = $("[name=roleId]:checked");
            if (rolelRadio == undefined || rolelRadio.length <= 0) {
                layer.alert("请给该用户设置角色", {icon: 5});
                return false;
            }
            let loading = layer.load();
            $.ajax({
                url: '/my/user/save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
            return false;
        });

        //获取所有的角色列表
        function getAllRole(callback) {
            $.ajax({
                url: "/my/role/all",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = "";
                    if (result.data && result.data.length > 0) {
                        for (i in result.data) {
                            html += '<input type="radio"  name="roleId" value="' + result.data[i].role_id + '" title="' + result.data[i].role_name + '" >' +
                                '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>' + result.data[i].role_name + '</div></div>';
                        }
                        $(".show-role-container").html(html);
                        callback();
                    }
                }
            });
        }
    })
</script>
<script>
</script>
</body>
</html>